/* 
 * File:   LogSystem.cpp
 * Author: Gab'z
 * 
 * Created on 2 mai 2012, 23:21
 */

#include "LogSystem.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

std::string LogSystem::destination = "calculatrice.log";
int LogSystem::TRACE = 0;
int LogSystem::DEBUG = 1;
int LogSystem::ERROR = 2;

LogSystem::~LogSystem() {
}

void LogSystem::log(const LogMessage& message) {
    // mise en forme 
    std::string s;
    switch (message.getLevel()) {
        case 0: s = "TRACE";
            break;
        case 1: s = "DEBUG";
            break;
        case 2: s = "ERROR";
            break;
    }
    s += " : " /*+ date + " - "*/ + message.getMessage() + "\n";
    LogSystem::logConsole(s);
    LogSystem::logToFile(s);
}

void LogSystem::logToFile(const std::string& message) {
    ofstream fichier(destination.c_str(), ios::out | ios::app);

    if (!fichier.fail()) {
        //cout << "Le fichier " << destination << " n'existe pas !" << endl;
        fichier << message;
        fichier.close();


    } else cerr << "Impossible d'ouvrir le fichier !" << endl;
}

void LogSystem::logConsole(const std::string& message) {
    std::cerr << message;
}

